require "rubygems"
require "capistrano"

module Wondercap

  class MultiConfiguration
    include Enumerable
    
    attr_accessor :extension_modules, :initializers
    
    def initialize(extension_modules = [], initializers = [])
      @configurations = Hash.new { |h, k| h[k] = [] }
      @extension_modules = extension_modules
      @initializers = initializers
    end
    
    def add_configuration_defining_block(name, block)
      @configurations[name.to_sym] << block
    end
    
    def find_configuration_defining_blocks(name)
      key = name.to_sym
      @configurations.key?(key) ? @configurations[key] : nil
    end
    
    def delete_configuration_defining_blocks(name)
      @configurations.delete name.to_sym
    end
    
    def create_orphaned_configuration(name)
      config = Capistrano::Configuration.new
      @extension_modules.each do |mod| 
        class << config
          self
        end.instance_eval do
          include mod
        end
      end
      @initializers.each do |initializer|
        initializer.call(name.to_sym, config)
      end
      config      
    end
    
    def is_defined?(name)
      @configurations.key?(name.to_sym)
    end
    
    def names
      @configurations.keys
    end
    
  end
  
end
